package com.entrogy.dao;

import com.entrogy.bean.*;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface AttendanceMapper {
	@Select("SELECT * FROM arrange_information WHERE arrange_time = #{currentDate}")
	ArrangeInformationEntity getArrangeInfor(@Param("currentDate") String currentDate);

	//此处表里是course_id实际代表年级id
	@Select("SELECT * FROM ${tableName} WHERE school_code = #{schoolCode} and course_id = #{courseId}")
	ArrangeEntity getDynArrangeBean(@Param("tableName") String tableName, @Param("courseId") int courseId,@Param("schoolCode") int schoolCode);

	@Select("SELECT * FROM leave_record WHERE student_id=#{id} AND isauthority=1 AND pass_times>0 ORDER BY start_time DESC")
	List<LeaveRecordEntity> getLeaveBeanListById(@Param("id") int id);

	@Update("UPDATE leave_record SET pass_times=0 WHERE leave_id=#{leaveId}")
	int cancelLeaveRecordByLeaveId(@Param("leaveId") int leave_id);

	@Insert("INSERT INTO arrange_information(arrange_time,arrange_table,cycle_table,teacher_id,create_time) VALUES(#{date},#{tableArrange}," +
			"#{tableCycle},#{teacherId},#{curDateTime})")
	int addArrangeInfor(@Param("date") String date,@Param("tableArrange") String tableArrange, @Param("tableCycle") String tableCycle, @Param("teacherId") String teacherId, @Param("curDateTime") String curDateTime);

	@Update("CREATE TABLE ${tableName} ( `course_id` int(11) NOT NULL," +
			"`day_id` int(11) NOT NULL,`afternoon` varchar(255) DEFAULT NULL," +
			"`evening` varchar(255) DEFAULT NULL,`night` varchar(255) DEFAULT NULL," +
			" `whichday` varchar(255) DEFAULT NULL,school_code int(11) NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8;")
	void createDynArrange(@Param("tableName") String tableArrange);

    @Insert("Insert into ${tableName}(course_id,day_id,afternoon,evening,night,whichday,school_code) " +
			"SELECT course_id,day_id,afternoon,evening,night,whichday,school_code FROM static_arrange WHERE whichday like '%${curWeekNum}%' and school_code = #{schoolCode} ORDER BY course_id")
	int getStaticArrangeToDyn(@Param("tableName") String tableArrange, @Param("curWeekNum") int curWeekNum,@Param("schoolCode") int schoolCode);
    @Update("CREATE TABLE  ${tableCycle}  ( `course_id` int(11) NOT NULL," +
			"`out_type` varchar(255) NOT NULL DEFAULT '',`cycle_id` int(11) DEFAULT NULL," +
			" `cycle_name` varchar(255) DEFAULT NULL,`type` int(11) DEFAULT NULL," +
			"`timeslot` varchar(255) DEFAULT NULL,`school_code` int(11) NOT NULL" +
			") ENGINE=InnoDB DEFAULT CHARSET=utf8;")
	int createDynCycle(@Param("tableCycle") String tableCycle);
	@Insert("INSERT INTO ${tableCycle} SELECT * FROM static_cycle WHERE school_code = #{schoolCode} ")
	void insetDynCycle(@Param("tableCycle") String tableCycle,@Param("schoolCode") int schoolCode);

    @Update("UPDATE  ${tableCycle} SET type=#{type}, timeslot = #{timeSlot} WHERE school_code = #{schoolCode} AND course_id = #{courseId} AND out_type=#{outType}")
	void updateDynCycle(@Param("tableCycle") String tableCycle, @Param("type") int type, @Param("timeSlot") String timeSlot,@Param("courseId") int course_id,@Param("outType") String outType,@Param("schoolCode") int schoolCode);

    @Select("SELECT * FROM ${cycleTable} WHERE school_code = #{schoolCode} and course_id = #{courseId}")
	List<CycleEntity> getCycleList(@Param("cycleTable") String cycleTable, @Param("courseId") int course_id,@Param("schoolCode") int schoolCode);

    @Update("UPDATE leave_record SET ispushtime=1 WHERE leave_id=#{leaveId}")
	int doNotPushTime(@Param("leaveId") int leaveId);

    @Select("SELECT * FROM student_information WHERE school_code = #{schoolCode} and course_id LIKE '${courseId}%' AND allow_time=#{outType}")
	List<StudentEntity> getStudentListByCourseIdAndOutType(@Param("courseId") int courseId, @Param("outType") String outType,@Param("schoolCode") int schoolCode);

    @Select("SELECT * from leave_record where student_id=#{stuId} AND isauthority=1 AND pass_times>0 AND unix_timestamp(start_time)<#{endTime} AND unix_timestamp(end_time)>#{endTime}")
	List<StudentEntity> needPushTime(@Param("stuId") int stuId, @Param("endTime") long endTime);

    @Select("SELECT * FROM ${tableCycle} WHERE school_code = #{schoolCode} AND course_id=#{gradeId} AND out_type=#{allowTime}")
	CycleEntity getCycleArrangeList(@Param("tableCycle") String tableCycle, @Param("gradeId") int gradeId,@Param("allowTime") String allow_time,@Param("schoolCode") int schoolCode);

    @Select("SELECT * FROM ${tableName}")
	List<ArrangeEntity> getDynArrangeBeanList(@Param("tableName") String tableName);

    @Select("SELECT * FROM leave_record WHERE isauthority=1 AND pass_times>0 AND ispushtime=0 ORDER BY start_time DESC")
	List<LeaveRecordEntity> getValidLeaveListWithoutPushTime();

    @Select("SELECT * FROM student_information WHERE id=#{id}")
	StudentEntity getStudentBeanById(@Param("id") int id);

	@Select("SELECT distinct day_id FROM ${tableName} WHERE school_code=#{schoolCode}")
    List<Integer> listAllDayType(@Param("tableName") String tableName, @Param("schoolCode") Integer schoolCode);
}
